package com.sunricher.telinkblemeshlib.models;

import android.os.Build;
import android.util.Log;

import com.sunricher.telinkblemeshlib.MeshCommand;

import java.util.ArrayList;
import java.util.HashMap;

public class NaturalLight {

    public enum Mode {
        mode1, mode2, mode3, mode4
    }

    public static int getModeValue(Mode mode) {
        switch (mode) {
            case mode2:
                return 2;
            case mode3:
                return 3;
            case mode4:
                return 4;
            default:
                return 1;
        }
    }

    public static Mode getMode(int modeValue) {
        switch (modeValue) {
            case 2:
                return Mode.mode2;
            case 3:
                return Mode.mode3;
            case 4:
                return Mode.mode4;
            default:
                return Mode.mode1;
        }
    }

    public enum Template {
        standard, office, healthCare
    }

    public static class Item {
        private int hour = 0;
        private int minute = 0;
        private int brightness = 100;
        private int cct = 45;

        public int getHour() {
            return hour;
        }

        /**
         * hour range [0, 23]
         */
        public void setHour(int hour) {
            this.hour = hour;
        }

        public int getBrightness() {
            return brightness;
        }

        /**
         * brightness range [0, 100]
         */
        public void setBrightness(int brightness) {
            this.brightness = brightness;
        }

        public int getCct() {
            return cct;
        }

        /**
         * cct range [0, 100]
         */
        public void setCct(int cct) {
            this.cct = cct;
        }

        public int getMinute() {
            return minute;
        }

        /**
         * minute range [0, 59]
         */
        public void setMinute(int minute) {
            this.minute = minute;
        }

        private int getKelvin() {
            return NaturalLight.getKelvinFromCct(cct);
        }

        private void setKelvin(int kelvin) {
            cct = NaturalLight.getCctFromKelvin(kelvin);
        }
    }

    private ArrayList<Item> items = new ArrayList<>();

    public static final String LOG_TAG = "NaturalLight";

    public NaturalLight() {

        if (CT_KELVIN_VALUES == null) {
            Log.e(LOG_TAG, "CT_KELVIN_VALUES is null");
        }
        if (KELVIN_CT_VALUES == null) {
            Log.e(LOG_TAG, "KELVIN_CT_VALUES is null");
        }
    }

    public static HashMap<Integer, Integer> CT_KELVIN_VALUES;
    static {
        CT_KELVIN_VALUES = new HashMap<>();
        CT_KELVIN_VALUES.put(100, 2000);
        CT_KELVIN_VALUES.put(90, 2500);
        CT_KELVIN_VALUES.put(80, 3000);
        CT_KELVIN_VALUES.put(70, 3500);
        CT_KELVIN_VALUES.put(60, 4000);
        CT_KELVIN_VALUES.put(45, 4500);
        CT_KELVIN_VALUES.put(30, 5000);
        CT_KELVIN_VALUES.put(20, 5500);
        CT_KELVIN_VALUES.put(10, 6000);
        CT_KELVIN_VALUES.put(0, 6500);
    }

    public static HashMap<Integer, Integer> KELVIN_CT_VALUES;
    static {
        KELVIN_CT_VALUES = new HashMap<>();
        KELVIN_CT_VALUES.put(2000, 100);
        KELVIN_CT_VALUES.put(2500, 90);
        KELVIN_CT_VALUES.put(3000, 80);
        KELVIN_CT_VALUES.put(3500, 70);
        KELVIN_CT_VALUES.put(4000, 60);
        KELVIN_CT_VALUES.put(4500, 45);
        KELVIN_CT_VALUES.put(5000, 30);
        KELVIN_CT_VALUES.put(5500, 20);
        KELVIN_CT_VALUES.put(6000, 10);
        KELVIN_CT_VALUES.put(6500, 0);
    }

//    public final HashMap<Integer, Integer> CT_KELVIN_VALUES = new HashMap<Integer, Integer>();
//
//    public final HashMap<Integer, Integer> KELVIN_CT_VALUES = new HashMap<Integer, Integer>();

    /**
     * ct: 0, 10, 20, 30, 45, 60, 70, 80, 90, 100.
     * <p>
     * If cct is out of range, will return 4500K.
     */
    public static int getKelvinFromCct(int cct) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (CT_KELVIN_VALUES != null && CT_KELVIN_VALUES.containsKey(cct)) {
                return CT_KELVIN_VALUES.getOrDefault(cct, 4500);
            }
        }
        return 4500;
    }

    /**
     * kelvin: 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500
     * <p>
     * If kelvin is out of range, will return 45.
     */
    public static int getCctFromKelvin(int kelvin) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (KELVIN_CT_VALUES != null && KELVIN_CT_VALUES.containsKey(kelvin)) {
                return KELVIN_CT_VALUES.getOrDefault(kelvin, 45);
            }
        }
        return 45;
    }

    public static NaturalLight makeTemplateNaturalLight(Template template) {
        NaturalLight naturalLight = new NaturalLight();
        int[] kelvins = new int[]{};
        int[] bris = new int[]{};
        switch (template) {
            case standard:
                kelvins = new int[]{
                        2000, 2000, 2500, 2500,
                        3000, 3000, 3500, 3500,
                        4000, 4500, 5000, 5500,
                        6000, 6500, 6000, 5500,
                        5000, 4500, 4000, 3500,
                        3000, 2500, 2000, 2000
                };
                bris = new int[]{
                        5, 5, 10, 10,
                        10, 10, 25, 25,
                        25, 75, 75, 100,
                        100, 100, 100, 100,
                        75, 75, 25, 25,
                        10, 10, 5, 5
                };
                break;
            case office:
                kelvins = new int[]{
                        4500, 4500, 4500, 4500,
                        4500, 4500, 4500, 5000,
                        5000, 5500, 6000, 6000,
                        6000, 5500, 5500, 5000,
                        5000, 5000, 5000, 4500,
                        4500, 4500, 4500, 4500
                };
                bris = new int[]{
                        10, 10, 10, 10,
                        10, 10, 10, 100,
                        100, 100, 100, 100,
                        100, 100, 100, 100,
                        100, 100, 100, 100,
                        100, 100, 10, 10
                };
                break;
            case healthCare:
                kelvins = new int[]{
                        2500, 2500, 2500, 2500,
                        2500, 2500, 2500, 2500,
                        2500, 3500, 4500, 4500,
                        5000, 5500, 5500, 5000,
                        5000, 4500, 4500, 4000,
                        3000, 2500, 2500, 2500
                };
                bris = new int[]{
                        10, 10, 10, 10,
                        10, 10, 10, 10,
                        10, 50, 75, 90,
                        100, 100, 100, 90,
                        80, 70, 60, 50,
                        20, 10, 10, 10
                };
                break;
        }
        for (int hour = 0; hour <= 23; hour++) {
            Item item = new Item();
            item.hour = hour;
            item.setKelvin(kelvins[hour]);
            item.brightness = bris[hour];
            naturalLight.items.add(item);
        }
        return naturalLight;
    }

    public ArrayList<Item> getItems() {
        return items;
    }

    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }
}
